var cf = require("../../../config.js");
var cusmallToken = wx.getStorageSync('cusmallToken');
var baseHandle = require("../../template/baseHandle.js");
//获取应用实例
var app = getApp();
Page(Object.assign({}, baseHandle, {

  /**
   * 页面的初始数据
   */
  data: {
    extConfig: wx.getExtConfigSync ? wx.getExtConfigSync() : {},
    app: app,
	eId: "",
	labels: [],
	oldLN: "",
	labName: "",
	showLabModal: false,
	modalType: "add"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	var ctx = this;
    app.getUserInfo(this, options, function(userInfo, res) {
      cusmallToken = wx.getStorageSync('cusmallToken');
      ctx.setData({
    	 eId: options.id 
      });
    });
    ctx.fetchData();
  },
  
  fetchData: function() {
	  var ctx = this;
	  wx.showLoading({
		  title: '加载中',
	  });
	  wx.request({
		  url: cf.config.pageDomain + '/applet/mobile/visitcard/employee/getEmployeeInfo',
		  data: {
			  cusmallToken: cusmallToken,
		  },
		  header: {
			  'content-type': 'application/json'
		  },
		  success: function(res) {
			  var data = res.data;
			  if (data && data.ret == 0) {
				  if ("" != data.model.employee.labels) {
					  data.model.employee.labels = JSON.parse(data.model.employee.labels);
				  } else {
					  data.model.employee.labels = [];
				  }
				  ctx.setData({
					  labels: data.model.employee.labels,
				  });
			  } else {
			  }
		  },
		  fail: function(res) {
			  
		  },
		  complete: function() {
			  wx.hideLoading();
		  }
	  });
  },
  
  labNameChange: function (e) {
    this.setData({
    	labName: e.detail.value
    });
  },
  
  openAddModal: function (e) {
	  this.data.modalType = e.currentTarget.dataset.modalType;
	  this.setData({
		  labName: "",
		  showLabModal: true
	  });
  },
  
  openEditModal: function (e) {
	  var oldLN = e.currentTarget.dataset.ln;
	  this.data.modalType = e.currentTarget.dataset.modalType;
	  this.setData({
		  oldLN: oldLN,
		  labName: oldLN,
		  showLabModal: true
	  });
  },
  
  openDelCfmModa: function (e) {
	  var ctx = this;
	  var delLN = e.currentTarget.dataset.ln;
	  wx.showModal({
		 title: "提示",
		 content: "您确定要删除吗？",
		 success: function (res) {
			if (res.confirm) {
				ctx.setData({
					labName: delLN
				});
				ctx.delLab();
			}
		 }
	  });
  },
  
  modalCancel: function () {
	this.setData({
		showLabModal: false
	});  
  }, 
  
  modalConfirm: function () {
	if ("add" == this.data.modalType) {
		this.addLab();
	} else if ("update" == this.data.modalType) {
		this.updateLab();
	}
  },
  
  addLab: function () {
	  var ctx = this;
	  if (15 < ctx.data.labels.length) {
		  wx.showModal({
			  title: "提示",
			  content: "标签个数不能超过15",
			  showCancel: false
		  });
		  return;
	  }
	  if ("" == ctx.data.labName) {
		  wx.showModal({
			  title: "提示",
			  content: "标签名不能为空",
			  showCancel: false
		  });
		  return;
	  }
	  wx.showLoading({
		  title: "加载中"
	  });
	  wx.request({
		  url: cf.config.pageDomain + "/applet/mobile/visitcard/employee/updateLabels",
		  data: {
			  cusmallToken: cusmallToken,
			  id: ctx.data.eId,
			  addLabel: ctx.data.labName
		  },
		  header: {
			  "Content-Type": "application/json"
		  },
		  success: function (res) {
			  if (res.data && 0 == res.data.ret) {
				  ctx.setData({
					  showLabModal: false
				  });
				  ctx.fetchData();
			  } else {
				  wx.showModal({
					 title: "提示",
					 content: res.data.msg
				  });
			  }
		  },
		  fail: function (res) {
			  
		  },
		  complete: function (res) {
			  wx.hideLoading();
		  },
	  });
  },
  
  updateLab: function () {
	  var ctx = this;
	  if ("" == ctx.data.labName) {
		  wx.showModal({
			  title: "提示",
			  content: "标签名不能为空",
			  showCancel: false
		  });
		  return;
	  }
	  wx.showLoading({
		  title: "加载中"
	  });
	  wx.request({
		  url: cf.config.pageDomain + "/applet/mobile/visitcard/employee/updateLabels",
		  data: {
			  cusmallToken: cusmallToken,
			  id: ctx.data.eId,
			  updateOldLabel: ctx.data.oldLN,
			  updateNewLabel: ctx.data.labName
		  },
		  header: {
			  "Content-Type": "application/json"
		  },
		  success: function (res) {
			  if (res.data && 0 == res.data.ret) {
				  ctx.setData({
					  showLabModal: false
				  });
				  ctx.fetchData();
			  } else {
				  wx.showModal({
					 title: "提示",
					 content: res.data.msg,
					 showCancel: false
				  });
			  }
		  },
		  fail: function (res) {
			  
		  },
		  complete: function (res) {
			  wx.hideLoading();
		  },
	  });
  },
  
  delLab: function () {
	  var ctx = this;
	  wx.showLoading({
		  title: "加载中"
	  });
	  wx.request({
		  url: cf.config.pageDomain + "/applet/mobile/visitcard/employee/updateLabels",
		  data: {
			  cusmallToken: cusmallToken,
			  id: ctx.data.eId,
			  delLabel: ctx.data.labName
		  },
		  header: {
			  "Content-Type": "application/json"
		  },
		  success: function (res) {
			  if (res.data && 0 == res.data.ret) {
				  ctx.setData({
					  showLabModal: false
				  });
				  ctx.fetchData();
			  } else {
				  wx.showModal({
					 title: "提示",
					 content: res.data.msg,
					 showCancel: false
				  });
			  }
		  },
		  fail: function (res) {
			  
		  },
		  complete: function (res) {
			  wx.hideLoading();
		  },
	  });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
	  var ctx = this;
	  let pages = getCurrentPages();//当前页面
      let prevPage = pages[pages.length - 2];//上一页面
      prevPage.setData({//直接给上移页面赋值
        ["cardInfo.labels"]: ctx.data.labels
      });
  },
}))